package com.meilishuo.ios.performance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xuemeng Wang on 14-10-30.
 * 解析抓取的ios内存 cpu信息，top.dat
 */
public class TopInfo {
    private static final Logger logger = LoggerFactory.getLogger(TopInfo.class);
    private static final String JSCHARTPATH = "target/ios-info/html/";
    private static final String MEMFILEPATH = "target/ios-info/dat/top.dat";

    List<String> cpuList = new ArrayList<String>();
    List<String> memList = new ArrayList<String>();

    /**
     * 解析top.dat存储到List中
     */
    public void parseTopFile()
    {
        File file = new File(MEMFILEPATH);
        if(null==file) return;

        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = null;

            while((line=bufferedReader.readLine())!=null)
            {
                line = line.replace("%", "").replace("M+", "");
                String[] array = line.trim().split(" "); //空格分隔
                if(null==array || array.length!=2 || "".equals(array[1]))
                    continue;

                cpuList.add(array[0]);
                memList.add(array[1]);
            }
            System.out.println(cpuList.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将List中的数据写到到xml中
     */
    public void writeXmlFromMap(List<String> infoList, String type)
    {
        if(null==infoList || infoList.size()==0)
            return;

        //写入xml
        File file = new File(JSCHARTPATH+type+".xml");

        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            bufferedWriter.write(
                    "<?xml version=\"1.0\"?>\n" +
                            "<JSChart>\n" +
                            "\t<dataset type=\"line\">");
        } catch (IOException e) {
            e.printStackTrace();
        }

        int pos = 1;
        for(String index : infoList)
        {
            if(null==index)
                continue;
            try {
                bufferedWriter.write("<data unit=\""+pos+"\" value=\""+index+"\"/>\n");
                pos++;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            bufferedWriter.write(
                    "</dataset>\n" +
                            "\t<optionset>\n" +
                            "\t\t<option set=\"setLineColor\" value=\"'#8D9386'\"/>\n" +
                            "\t\t<option set=\"setLineWidth\" value=\"4\"/>\n" +
                            "\t\t<option set=\"setTitleColor\" value=\"'#7D7D7D'\"/>\n" +
                            "\t\t<option set=\"setAxisColor\" value=\"'#9F0505'\"/>\n" +
                            "\t\t<option set=\"setGridColor\" value=\"'#a4a4a4'\"/>\n" +
                            "\t\t<option set=\"setAxisValuesColor\" value=\"'#333639'\"/>\n" +
                            "\t\t<option set=\"setAxisNameColor\" value=\"'#333639'\"/>\n" +
                            "\t\t<option set=\"setTextPaddingLeft\" value=\"0\"/>\n" +
                            "\t\t<option set=\"setLineSpeed\" value=\"100\"/>\n" +
                            "\t\t<option set=\"setBarSpeed\" value=\"100\"/>\n" +
                            "\t</optionset>\n " +
                            "</JSChart>");
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 生成jschart html文件
     */
    public void writeJsHtml(String type)
    {
        File xmlFile = new File(JSCHARTPATH+type+".xml");

        File htmlFile = new File(JSCHARTPATH+type+".html");
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(htmlFile));
                bufferedWriter.write(
                        "<html>\n" +
                                "<head>\n" +
                                "\n" +
                                "<title>"+type+"</title>\n" +
                                "\n" +
                                "<script type=\"text/javascript\" src=\"jscharts.js\"></script>\n" +
                                "\n" +
                                "</head>\n" +
                                "<body>\n" +
                                "<div id=\"result\">\n" +
                                "<h3>"+type+"</h3>" +
                                "<div id=\"graph\">Loading graph...</div>\n" +
                                "\n" +
                                "<script type=\"text/javascript\">\n" +
                                "\t\n" +
                                "\tvar myChart = new JSChart('graph', 'line');\n" +
                                "\tmyChart.setDataXML(\""+xmlFile.getName()+"\");\n" +
                                "\tmyChart.draw();\n" +
                                "\t\n" +
                                "</script>\n" +
                                "\n" +
                                "\n" +
                                "</div>\n" +
                                "</body>\n" +
                                "</html>\n"
                );
                bufferedWriter.flush();
                bufferedWriter.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public List<String> getCpuList() {
        return cpuList;
    }

    public List<String> getMemList() {
        return memList;
    }

    @Test
    public void generateIosHtmlInfo()
    {
        TopInfo topInfo = new TopInfo();
        topInfo.parseTopFile();
        topInfo.writeXmlFromMap(topInfo.getCpuList(), "cpu");
        topInfo.writeXmlFromMap(topInfo.getMemList(), "mem");
        topInfo.writeJsHtml("cpu");
        topInfo.writeJsHtml("mem");
    }
}
